﻿using System.Collections.Generic;
using UnityEngine;

// Version 1.1
// 调用PageEvent来实现页面显示与隐藏控制

public class PageCtrl : MonoBehaviour
{
	// PageEvent字典，通过页面关键字调用
	public Dictionary<string, PageEvent> pageCtrls = new Dictionary<string, PageEvent>();
	[Header("参数浏览")]
	[Tooltip("显示控制层名称")]
	public List<string> pageCtrlName = new List<string>();
	[Tooltip("进来页名称")]
	public string come_page;
	[Tooltip("离开页名称")]
	public string go_page;

	void Awake()
	{
		GameObject[] pages = GameObject.FindGameObjectsWithTag("Page");

		for (int i = 0; i < pages.Length; i++)
		{
			pageCtrls.Add(pages[i].name, pages[i].GetComponent<PageEvent>());
		}
	}

	/// <summary>
	/// 根据关键字显示某页
	/// </summary>
	/// <param name="key">页面关键字@显示参数， 特殊为go</param>
	public void Show(string key)
	{
		if (key == string.Empty)
		{
			print("关键字错误");
			return;
		}

		string[] keys = key.Split('@');
		string pg_key = null;
		if (keys.Length > 1)
		{
			pg_key = keys[1];
		}

		if (keys[0].Equals("go"))
		{
			//显示之前离开的页面，类似与返回
			keys[0] = go_page;
		}

		come_page = keys[0];
		if (pageCtrls.ContainsKey(come_page))
		{
			//print("显示页面:" + come_page);
			pageCtrls[keys[0]].Show(pg_key);
		}
		else
		{
			Debug.LogError("错误页面，未添加:" + come_page);
		}


	}

	/// <summary>
	/// 根据关键字离开某页
	/// </summary>
	/// <param name="key">页面关键字@离开参数， 特殊为come</param>
	public void Hide(string key)
	{
		if (key == string.Empty)
		{
			print("关键字错误");
			return;
		}

		string[] keys = key.Split('@');
		string pg_key = null;
		if (keys.Length > 1)
		{
			pg_key = keys[1];
		}
		
		if (keys[0].Equals("come"))
		{
			//离开之前来的页面
			keys[0] = come_page;
		}

		go_page = keys[0];
		if (pageCtrls.ContainsKey(go_page))
		{
			//print("关闭页面:" + go_page);
			pageCtrls[keys[0]].Hide(pg_key);
		}
		else
		{
			Debug.LogError("错误页面，未添加:" + come_page);
		}


	}
}
